<?php
class ApproveAction extends CommonAction {
	public function _filter(&$map) {
		
	}
	
	public function _before_index() {
		$userid = $_SESSION [C ( 'USER_AUTH_KEY' )];
		$model = M ( "Contract" );
		/* $result = $this->isDepart ();
			// 默认管理员的id是1
		if ($result || $userid == 1) {
		$this->assign ( 'flag', 'flag' );
		}
		// 审批权限
		$apps = $this->isApprove ( $userid );
		if ($apps && $result || $userid == 1) {
		$this->assign ( 'aflag', 'aflag' );
		} */
		/* $this->assign ( 'userid', $userid );
			/* 业务提醒 */
		$model=D('Contract');
		//得到所有合同中的合同来源
		$projectlist=$model->field('projectid')->select();
		$business=D('Business');
		$buslist=$business->field('id')->select();
		for($i=0;$i<count($projectlist);$i++){
			$project[$i]=$projectlist[$i]['projectid'];//得到合同来源的projectid
		}
		for($j=0;$j<count($buslist);$j++){
			$buss[$j]=$buslist[$j]['id'];//得到业务的id
		}
		//比较业务和合同来源的差值
		$different=array_diff($buss,$project);
		$difcount=count($different);
		/* dump($buss);
			dump($project);
		dump($different); */
		for($m=0;$m<=count($buss);$m++){
			if((int)$different[$m]!=0){
				$diflist[$m]=$business->where('id='.(int)$different[$m])->find();
			}
			//echo (int)$different[$m];
		}
		//dump($diflist);
		$this->assign('difcount',$difcount);
		$this->assign('diflist',$diflist);
	}
	public function index() {
		$state=$_REQUEST['state'];//高级检索状态判断
		$model = M ('Contract');
		$map['removed']=1;
		$this->_list ( $model,$map);
// 		import ( '@.Org.Page' );
// 		$count = $model->count ();
// 		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 20 );
// 		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
// 		$list = $model->page ( $pageNum . "," . $page->listRows )->select ();
// 		$this->assign ( 'list', $list );
// 		$this->assign ( 'totalCount', $count ); // 总条数
// 		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
// 		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display ();
		
		
	}
	// 审批
	public function approve() {
		$model = D ( 'Contract' );
		$connname = M('ContractGood');
		$id = $_REQUEST ['id'];
		$alshipment = $model->where ( 'id=' . $id )->getField ( 'alshipment' );
		$quality = $model->where ( 'id=' . $id )->getField ( 'quality' );
		$percent = ($alshipment / $quality) * 100;
		$this->assign ( 'percent', $percent );
		// $result=$model->where('id='.$id)->find();
		$result = $model->getById ( $id );
		$cinlist=$connname->where('contractid='.$id)->select();
		$this->assign('conname',$cinlist);
		$this->assign ( 'vo', $result );
		$this->assign('blockoutid',$_REQUEST['blockoutid']);
		$this->display ("approve");
	}
	
	//审批保存
	public function passapprove() {
	    
	    $contractlog=M('ContractLog');
	    $cgood=M('ContractGood');
	    $AL=M('ApprovalLog');
	    $AL->startTrans();//开启事务
	    $goodid=$_REQUEST['goodid'];
	    //权限表
	    $arruserid=explode(',',$_POST['userid']);
	    $model1=M('CustomerPayment');
		$model = D ( 'Contract' );
	//	$model3 = M("ControllogUser");
		$customerModel=M('Customer');
		$id = $_REQUEST ['id'];
		$vo=$model->where('id='.$id)->find();
		//本次放量数值
		$capacity = $_REQUEST ['capacity'];
		$Blockoutid=$_REQUEST['blockoutid'];
		$Blockout = $model1->find($Blockoutid);
		$result1=true;
		$result2=true;
		$result4=true;
		//可放量吨数
		$kflds = $Blockout['capacity'];
		//审批中已放量吨数
		$spalshipment=$Blockout['alshipment'];
		//计算后得出本次放量后的已放量吨数
		$afterspalshipment=bcadd($spalshipment,$capacity,4);
		$payondelivery=$_REQUEST['payondelivery'];
		$goods=$cgood->where('id='.$goodid)->find();
		$status = $model->where ( 'id=' . $id )->getField ( 'status' );
		if ($status == 100) {
			$AL->rollback();
			$this->ajaxReturn ( 0, '该合同已放量完毕！', 300 );
		} else {
			if($capacity>bcsub($kflds,$spalshipment,4)){
				$AL->rollback();
				$this->ajaxReturn ( 0, '输入放量吨数大于可审批吨数，请确认！', 300 );
			}
			
			//得到已放量吨数
			$alshipment = $cgood->where ( 'id=' . $goodid )->getField ( 'alshipment' );
			//得到剩余未吨数
			$quality = $cgood->where ( 'id=' . $goodid )->getField ( 'quality' );
			//得到剩余吨数
			$notshipment = $cgood->where ( 'id=' . $goodid )->getField ( 'notshipment' );
			if($capacity>$quality){
				$AL->rollback();
				$this->ajaxReturn(0,'输入的放量大于合同未放量吨数，请确认！',300);
			}
			$userid = $_SESSION [C ( 'USER_AUTH_KEY' )];
			
			$note = $_REQUEST ['note'];
			$data1 ['approveid'] = $userid;
			$data1 ['capacity'] = $capacity;
			/**
			 * 判断输入的放量值是否超过了余量
			 */
			//$res = $quality - $alshipment;
			if ($notshipment < $capacity || $capacity > $quality) {
				$AL->rollback();
				$this->ajaxReturn ( $notshipment, '输入的放量数大于余量', 300 );
			}
			$data1 ['note'] = $note;
			$data ['alshipment'] = bcadd($alshipment,$capacity,4);
			$data ['notshipment'] = bcsub($quality,(bcadd($alshipment,$capacity,4)),4);
			$shipment = bcadd($alshipment,$capacity,4);
			/* if ($quality == $shipment) {
				$data1 ['status'] = 200;//如果放量完毕状态改为200
			} else {
				$data1 ['status'] = 101;
			} */
			$result1=$cgood->where('id='.$goodid)->save($data);
			$data1 ['status'] = $this->getres($id);
			
			
// 			$conlist = $contractlog->where('controlid='.$id.' and goodname='.$goods['goodname'])->select();
// 			$j=count($conlist);
// 			if($j>0){
// 				$condata['capacity'] = bcadd($conlist[0]['capacity'],$capacity);
				
// 			}
			
			
			
			//生成拟定调度表，capacity指本次审批许调度的量
			$ctlogdata['capacity']=$capacity;
			//需要调度的商品id
			$ctlogdata['contractgoodid']=$goodid;
			//合同的ID
			$ctlogdata['contractid']=$id;
			//拟定审批的ID
			$ctlogdata['customerpaymentid']=$Blockoutid;
			//拟定审批的时间
			$ctlogdata['capatime']=date('YmdHis');
			
			//需要调度的商品名称
			$ctlogdata['goodname']=$goods['goodname'];
		    //客户名称
			$ctlogdata['secondname'] =$vo['secondname'];
			//客户id
			$ctlogdata['secondid'] =$vo['secondid'];
			//已调度吨数
			$ctlogdata['alshipment']=0;
			//到款金额
			$ctlogdata['receivedamount']=$_REQUEST['receivedamount'];
			//单价
			$ctlogdata['univalence']=$_REQUEST['univalence'];
			//备注
			$ctlogdata['note'] = $_REQUEST['note'];
		    //tpye 合同类型 包括客户合同，供应商合同 
			$ctlogdata['type'] = $vo['type'];
			//当选择货到付款时更改对应客户的payondelivery状态
			if($payondelivery==1){
				$secondname=$model->where('id='.$id)->getField('secondname');
				$map['payondelivery']=1;
				$result2 = $customerModel->where('customername=\''.$secondname.'\'')->save($map);
			}else{
				$secondname=$model->where('id='.$id)->getField('secondname');
				$map['payondelivery']=0;
				$result2 = $customerModel->where('customername=\''.$secondname.'\'')->save($map);
			}
			//添加客户供货信息
			//$this->insertCustomerSup();
// 		
			//$contractno = $model->where ( 'id=' . $id )->getField ( 'contractno' );
			$ctlogdata['contractno']=$vo['contractno'];
			if($vo['type']==1){
	
				$customer=$customerModel->where("customername='".$vo['secondname']."'")->select();
	            if(!empty($customer)){
	            	$custgood=M('CustomerGood');
	            	$custgoods=$custgood->where('customerid='.$customer[0]['id'].' and product=\''.$goods['goodname'].'\'')->select();
				     if(!empty($custgoods))
				     {
				     	$datagoods['hasexam']=bcadd($custgoods[0]['hasexam'],$capacity,4);
				     	$result4 = $custgood->where('customerid='.$customer[0]['id'].' and product=\''.$goods['goodname'].'\'')->save($datagoods);
				     }
	            }
			}
			$ctlogdata['iscommon']=0;
			//$iscommon=0;//拟定审批的状态为0 特殊审批款到卸车的状态为1, 特殊审批非款到卸车的状态为2,
			$CTLDATA=$contractlog->where('contractid='.$id.' and goodname=\''.$goods['goodname'].'\'  and iscommon=0')->select();
			$k = count($CTLDATA);
			if($k>0){
				$data2['capacity']= bcadd($CTLDATA[0]['capacity'],$capacity,4);
				$data2['status']=($CTLDATA[0]['alshipment']/$data2['capacity'])*100;
				$data2['iscommon']=0;
				$list=$contractlog->where('id='.$CTLDATA[0]['id'])->save($data2);
				$ctlogdata['contractlogid']= $CTLDATA[0]['id'];
			}else{
				$list = $contractlog->add($ctlogdata);
				$ctlogdata['contractlogid']= $list;
			}
			
			$result = $model->where ( 'id=' . $id )->data ( $data1 )->save ();
			//mlt_customer_payment表status字段当为100时，不再显示在拟定页面
			//本次放量后 已放量吨数
			$cpmap['alshipment']=$afterspalshipment;
			$cpmap['status']=($cpmap['alshipment']/$kflds)*100;
		    $result3 = $model1->where('id='.$Blockoutid)->save($cpmap); 
		    $Ral = $AL->add($ctlogdata);
			if ($Ral&&$result&&$result1&&$result3&&$result4&&$list) {
				$AL->commit();
// 				for($i=0;$i<count($arruserid);$i++){
// 					$data3['controllogid']=$list;
// 					$data3['userid']=$arruserid[$i];
// 					$model3->add($data3);
// 				}
				$this->ajaxReturn ( $result, '放量成功', 200 );
			} else {
				$AL->rollback();
				$this->ajaxReturn ( $result, '放量失败', 300 );
			}
		}
	}
	
	
	public function getgood()
	{
		
		$id=$_POST['id'];
		$model=M('ContractGood');
		$good=$model->where('id='.$id)->find();
		if(!empty($good))
		{
			$alshipment=$good['alshipment'];
			$quality=$good['quality'];
			if(!empty($alshipment)&&!empty($quality))
			{
				$precent=($alshipment/$quality)*100;
				$this->ajaxReturn ( $precent, '成功', 200 );
			}
			/* else
			{
				$precent=0;
			} */
		}
		$this->ajaxReturn ( 0, '失败', 300 );
	}
	
	//获得审批的商品的比例
	public function getres($id)
	{
		$cgood=M('ContractGood');
		
		$list=$cgood->where('contractid='.$id)->select();
		//$quality=0;
		if(!empty($list))
		{
			for ($i = 0; $i < count($list); $i++) {
				$quality+=$list[$i]['quality'];
				$alishiment+=$list[$i]['alshipment'];
			}
			if(!empty($quality)&&!empty($alishiment))
			{
				$precent=($alishiment/$quality)*100;
			}
			else
			{
				$precent=0;
			}
			
		}
		else 
		{
			$precent=0;
		}
		return $precent;
	}
	
	public function looklog()
	{
	  $id=$_REQUEST['id'];
	  $contractlog=M('ApprovalLog');
	  $list=$contractlog->where('contractid='.$id)->select();
	  $this->assign('list',$list);
	  $this->assign ( 'id', $id );
	  $this->display();
	  
	}
	
	public function addnote()
	{
		if("true"==$_REQUEST['insertNote']){
			$this->insertNote();
		}else{
		$id = $_REQUEST['id'];
		$this->assign('id',$id);
		$cg = M('Contract');
		$list=$cg->where('id='.$id)->getField('note');
		$this->assign('id',$id);
		$this->assign('note',$list);
		$this->display();
		}
	}
	public function insertNote()
	{
		$cg = M('Contract');
		$id = $_REQUEST['id'];
		$note=$_REQUEST['note'];
	
		//$list=$cg->where('id='.$id)->getField('note');
		$map['note']=$note;
		$result = $cg->where('id='.$id)->save($map);
		if($result){
			$this->ajaxReturn ( $result, '添加备注成功！', 200 );
		}else{
			$this->ajaxReturn ( $result, '添加备注失败！', 300 );
		}
	}
	/**
	 * 拟定审批
	 */
	public function BlockOutapprove(){
		if("true"==$_REQUEST['approve']){
			$this->approve();
		}else{
			if("true"==$_REQUEST['passapprove']){
				$this->passapprove();
			}else{
				if("true"==$_REQUEST['chooseReceiver']){
					$this->chooseReceiver();
				}else{
					$model = M ( 'CustomepaymentrUser' );
					$model1 = M ( 'CustomerPayment' );
					$model2 = M ( 'ControlTable' );
					$userid=$_SESSION [C ( 'USER_AUTH_KEY' )];
					//if('1'==$userid){
					//删除合同时,客户付款有记录的不显示
					$sql="select cusp.*  from mlt_customer_payment cusp ,mlt_contract c  where cusp. status>=0 and payondelivery!=1  and c.id=cusp.controlid  and c.removed=1 order by paytime desc";
						//$sql = "select * from mlt_customer_payment cusp where status!=100 and payondelivery!=1 order by paytime desc";
						
					//}else{
					//$sql = "select * from mlt_customer_payment cusp where status!=100 and payondelivery!=1 and  cusp.id in(select distinct customerpaymentid from mlt_customepaymentr_user  where userid=".$userid.") order by paytime desc";
					//}
					$result = $model1->query($sql);
					//dump($result);
					$this->assign('list',$result);
					$this->display();
				}
			}
		}
	}
	/*
	 * 付款后审批人带回
	*/
	public function chooseReceiver(){
		$User = M('User');
		$list=$User->select();
		$this->assign('list',$list);
		$this->display("chooseReceiver");
	}
	//特殊审批
	public function particularapprove(){
		if("true"==$_REQUEST['chooseReceiver']){
			$this->chooseReceiver();
		}else{
			if("true"==$_REQUEST['particlarpassapprove']){
			   $this->particlarpassapprove();
		     }else{
		     	$model = D ( 'Contract' );
		     	$connname = M('ContractGood');
		     	$id = $_REQUEST ['id'];
		     	$alshipment = $model->where ( 'id=' . $id )->getField ( 'alshipment' );
		     	$quality = $model->where ( 'id=' . $id )->getField ( 'quality' );
		     	$percent = ($alshipment / $quality) * 100;
		     	$this->assign ( 'percent', $percent );
		     	// $result=$model->where('id='.$id)->find();
		     	$result = $model->getById ( $id );
		     	$cinlist=$connname->where('contractid='.$id)->select();
		     	$this->assign('conname',$cinlist);
		     	$this->assign ( 'vo', $result );
		     	$this->assign('blockoutid',$_REQUEST['blockoutid']);
		     	$this->display ();
		     }
		}
		
	}
	//特殊调度提交处理
	public function particlarpassapprove(){
		$AL=M('ApprovalLog');
		$contractlog=M('ContractLog');
		$cgood=M('ContractGood');
		$goodid=$_REQUEST['goodid'];
		//权限表
		$arruserid=explode(',',$_POST['userid']);
		$model = D ( 'Contract' );
		//开启事务
		$AL->startTrans();
		//$model3 = M("ControllogUser");
		$result2 = true;
		$customerModel=M('Customer');
		$id = $_REQUEST ['id'];
		$vo=$model->where('id='.$id)->find();
		//本次放量数值
		$capacity = $_REQUEST ['capacity'];
		$Blockoutid=$_REQUEST['blockoutid'];
		
		$payondelivery=$_REQUEST['payondelivery'];
		$status = $model->where ( 'id=' . $id )->getField ( 'status' );
		if ($status == 100) {
			$AL->rollback();
			$this->ajaxReturn ( 1, '该合同已放量完毕！', 300 );
		} else {
			//得到已放量吨数
			$alshipment = $cgood->where ( 'id=' . $goodid )->getField ( 'alshipment' );
			//得到剩余未吨数
			$quality = $cgood->where ( 'id=' . $goodid )->getField ( 'quality' );
			//得到剩余吨数
			$notshipment = $cgood->where ( 'id=' . $goodid )->getField ( 'notshipment' );
			if($capacity>$quality){
				$AL->rollback();
				$this->ajaxReturn(0,'输入的放量大于合同未放量吨数，请确认！',300);
			}
			$userid = $_SESSION [C ( 'USER_AUTH_KEY' )];
				
			$note = $_REQUEST ['note'];
			$data1 ['approveid'] = $userid;
			$data1 ['capacity'] = $capacity;
			/**
			 * 判断输入的放量值是否超过了余量
			 */
			//$res = $quality - $alshipment;
			if ($notshipment < $capacity || $capacity > $quality) {
				$AL->rollback();
				$this->ajaxReturn ( $notshipment, '输入的放量数大于余量', 300 );
			}
			$data1 ['note'] = $note;
			$data ['alshipment'] = bcadd($alshipment,$capacity,4);
			$data ['notshipment'] = bcsub($quality,(bcadd($alshipment,$capacity,4)),4);
			$shipment = bcadd($alshipment,$capacity,4);
			/* if ($quality == $shipment) {
			 $data1 ['status'] = 200;//如果放量完毕状态改为200
			} else {
			$data1 ['status'] = 101;
			} */
			$result1=$cgood->where('id='.$goodid)->save($data);
			$data1 ['status'] = $this->getres($id);
			//生成拟定调度表，capacity指本次审批许调度的量
			$ctlogdata['capacity']=$capacity;
			//需要调度的商品id
			$ctlogdata['contractgoodid']=$goodid;
			//合同的ID
			$ctlogdata['contractid']=$id;
			//拟定审批的时间
			$ctlogdata['capatime']=date('YmdHis');
			$goods=$cgood->where('id='.$goodid)->find();
			//需要调度的商品名称
			$ctlogdata['goodname']=$goods['goodname'];
			//客户名称
			$ctlogdata['secondname'] =$vo['secondname'];
			//客户id
			$ctlogdata['secondid'] =$vo['secondid'];
			//已调度吨数
			$ctlogdata['alshipment']=0;
			//备注
			$ctlogdata['note'] = $_REQUEST['note'];
			//tpye 合同类型 包括客户合同，供应商合同
			$ctlogdata['type'] = $vo['type'];
			//当选择货到付款时更改对应你拟定调度的payondelivery状态
			if($payondelivery==1){
				$ctlogdata['payondelivery']=1;
				$ctlogdata['iscommon']=1;
			}else{
				$ctlogdata['payondelivery']=0;
				$ctlogdata['iscommon']=2;
			}
			//添加客户供货信息
			//$this->insertCustomerSup();
			//
			//$contractno = $model->where ( 'id=' . $id )->getField ( 'contractno' );
			$ctlogdata['contractno']=$vo['contractno'];
					if($vo['type']==1){
		
					$customer=$customerModel->where("customername='".$vo['secondname']."'")->select();
					if(!empty($customer)){
					 
					$custgood=M('CustomerGood');
						$custgoods=$custgood->where('customerid='.$customer[0]['id'].' and product=\''.$goods['goodname'].'\'')->select();
				     if(!empty($custgoods))
						     {
						     $datagoods['hasexam']=$custgoods[0]['hasexam']+$capacity;
						     $result2 =  $custgood->where('customerid='.$customer[0]['id'].' and product=\''.$goods['goodname'].'\'')->save($datagoods);
		
						     }
		
						     }
						     }else{
						     	if($vo['type']==2){
						     		$result2=true;
						     	}
						     }
						     	
						     	
						     $result = $model->where ( 'id=' . $id )->data ( $data1 )->save ();
						     //$iscommon=0;//拟定审批的状态为0 特殊审批款到卸车的状态为1, 特殊审批非款到卸车的状态为2,
						     if($payondelivery){
						     	$CTLDATA=$contractlog->where('contractid='.$id.' and goodname=\''.$goods['goodname'].'\'  and iscommon=1')->select();
						     }else{
						     	$CTLDATA=$contractlog->where('contractid='.$id.' and goodname=\''.$goods['goodname'].'\'  and iscommon=2')->select();
						     }
						    //  $CTLDATA=$contractlog->where('contractid='.$id.' and goodname=\''.$goods['goodname'].'\'  and iscommon=1')->select();
						     $k = count($CTLDATA);
						     if($k>0){
						     	$data2['capacity']= bcadd($CTLDATA[0]['capacity'],$capacity,4);
						     	$data2['status']=($CTLDATA[0]['alshipment']/$data2['capacity'])*100;
						     //	$data2['iscommon']=0;
						     	$list=$contractlog->where('id='.$CTLDATA[0]['id'])->save($data2);
						     	$ctlogdata['contractlogid']= $CTLDATA[0]['id'];
						     }else{
						     	$list = $contractlog->add($ctlogdata);
						     	$ctlogdata['contractlogid']= $list;
						     }
						     $RAL = $AL->add($ctlogdata);
						     if ($RAL&&$result&&$result1&&$result2&&$list) {
						    // $list = $contractlog->add($ctlogdata);
// 						     for($i=0;$i<count($arruserid);$i++){
// 						     $data3['controllogid']=$list;
// 						     		$data3['userid']=$arruserid[$i];
// 						     		$model3->add($data3);
// 						     }
						     $AL->commit();
						     $this->ajaxReturn ( $result, '放量成功', 200 );
					} else {
						$AL->rollback();
					    $this->ajaxReturn ( $result, '放量失败', 300 );
					}
				}		
	}
	/**
	 * 删除放量记录
	 */
	public function deletecontrollog(){
		$id = $_REQUEST['id'];
		$ids =''.$id;
		$model=M('ControlTable');
		$model3 = M('Unload');
		$model2 = M('ApprovalLog');
		$model2->startTrans();
		$model1=M('ContractLog');
		$model4=M('CustomerPayment');
		$APlist = $model2->find($id);
		$CTLlist = $model1->where('contractid='.$APlist['contractid'].' and contractgoodid='.$APlist['contractgoodid'].' and iscommon='.$APlist['iscommon'])->select();
		$CPTlist = $model4->where('controlid='.$APlist['contractid'])->select();
		
		//判断调度是否存在
// 		if(!empty($CTLlist))
// 		{
// 			$iscontable=$model->where('contractlogid='.$CTLlist[0]['id'])->select();
// 			if(!empty($iscontable))
// 			{
// 				$this->ajaxReturn(0,'调度已存在，不能删除',300);
// 			}
// 		}
	
		//放量吨数
		$capacity = $APlist['capacity'];
		$dataAp['capacity'] = bcsub($CTLlist[0]['capacity'],$capacity,4);
		$dataAp['status'] = ($CTLlist[0]['alshipment']/$dataAp['capacity'])*100;
		//还原拟定调度的可调度数
		if(bcsub($CTLlist[0]['alshipment'],$dataAp['capacity'],4)>0){
			$model2->rollback();
			$this->ajaxReturn(0,"删除后可调度吨数小于已调度吨数，不能删除",300);
		}
		$result = $model1->where('contractid='.$APlist['contractid'].' and contractgoodid='.$APlist['contractgoodid'].' and iscommon='.$APlist['iscommon'])->save($dataAp);
		
		$dataCPT['alshipment'] = bcsub($CPTlist[0]['alshipment'],$capacity,4);
		$dataCPT['status']=($dataCPT['alshipment']/$CPTlist[0]['capacity'])*100;
		$result3 = $this->UpwardReductionCustomer($id);
		//还原拟定审批的已审批数  拟定调度中的 如果是
		if(!empty($CPTlist)&&$APlist['iscommon']==0){
		$result1 = $model4->where('id='.$CPTlist[0]['id'])->save($dataCPT);
		}
		else
		{
			$result1=true;
		}
		
		$result2 = $model2->where('id='.$id)->delete();
		if($result2&&$result1&&$result&&$result3){
			$model2->commit();
			$this->ajaxReturn(1,'删除成功',200);
		}else{
			$model2->rollback();
			$this->ajaxReturn(0,'删除失败',300);
		}
//		$ControlTable = $model->where('contractlogid='.$id)->select();
// 		for($i=0;$i<count($ControlTable);$i++){
// 			$Unloaddata = $model3->getByOrders ( $ControlTable[$i] ['orders'] );
// 			if($Unloaddata){
// 				$this->ajaxReturn ( '0', "相关调度信息已经卸过车，不能删除调度信息！", '300' );
// 			}
// 		}
		//$CtTAction = new ControlTableAction();
		//$CtTAction->UpwardReductionCustomerPayment($ids);
		
		//$CtTAction->fordelete($id);
	}
	/**
	 * 
	 * 
	 */
	public function UpwardReductionCustomer($id){
		//还原合同的状态contractid
		$model = M('ApprovalLog');
		//由上级调度方法提交事务
		//$model->startTrans();
		$model1 = M('Contract');
		$cgood=M('ContractGood');
		$custgood=M('CustomerGood');
		$RCusGood = true;
		$ApprovalLog = $model->find($id);
		$goodid=$ApprovalLog['contractgoodid'];
 		//本次放量吨数
 		$capacity = $ApprovalLog['capacity'];
 		//得到已放量吨数
 		$alshipment = $cgood->where ( 'id=' . $goodid )->getField ( 'alshipment' );
 		//得到剩余未吨数
 		$quality = $cgood->where ( 'id=' . $goodid )->getField ( 'quality' );
 		//得到剩余吨数
 		$notshipment = $cgood->where ( 'id=' . $goodid )->getField ( 'notshipment' );
		$data1 ['alshipment'] = bcsub($alshipment,$capacity,4);
		$data1 ['notshipment'] = bcsub($quality,$data1 ['alshipment'],4);
		$RCGood =$cgood->where('id='.$goodid)->save($data1);
         $date['status'] = $this->getres($ApprovalLog['contractid']);
         $RCTT = $model1->where ( 'id=' . $ApprovalLog['contractid'])->save ($date);
         $custgoods=$custgood->where('customerid='.$ApprovalLog['secondid'].' and product=\''.$ApprovalLog['goodname'].'\'')->select();
         if(!empty($custgoods))
         {
        	$datagoods['hasexam']=bcsub($custgoods[0]['hasexam'],$capacity,4);
         	$RCusGood = $custgood->where('customerid='.$ApprovalLog['secondid'].' and product=\''.$ApprovalLog['goodname'].'\'')->save($datagoods);
         }
        if($RCGood&&$RCTT&&$RCusGood){
            return true;
        }else{
        	 return false;
        }
	}
	/**
	 * 修改审批记录
	 */
	public function editcontrollog(){
		$id = $_REQUEST['id'];
		$model = M('ContractLog');
		$Conlog = $model->find($id);
		$User = M('User');
		$u=$User->select();
		$this->assign('user',$u);
		$this->assign('vo',$Conlog);
		$this->display();
	}
	/**
	 * 保存审批记录的修改
	 */
	public function updatecontrollog(){
		$id = $_REQUEST['id'];
		$data['receivedamount'] = $_REQUEST['receivedamount'];
		$data['univalence'] = $_REQUEST['univalence'];
		$data['capacity'] = $_REQUEST['capacity'];
		$model = M('ContractLog');
		$result = $model->where('id='.$id)->save($data);
		if($result){
			$this->ajaxReturn($result,'更新成功',200);
		}else{
			$this->ajaxReturn($result,'更新失败',300);
		}
	}
}

